package org.springblade.protocol.tcp.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springblade.cache.DeviceManagerCache;
import org.springblade.common.constant.ProtocolConstant;
import org.springblade.entity.Product;
import org.springblade.handle.MsgDispatcher;

import java.util.Date;

/**
 * SocketClientHandle
 * @author lijiamin
 */
public class SocketClientHandle extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		super.channelRegistered(ctx);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		String address = ctx.channel().remoteAddress().toString().substring(1).split(":")[0];
		String port = ctx.channel().remoteAddress().toString().substring(1).split(":")[1];
		SocketClient.setOrDelServerChannel(address + ":" + port, ctx.channel());
		Product product = createFixProduct(address, port, 0);
		DeviceManagerCache.getDeviceManagerClient().updateProtocolStatus(product);
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		String address = ctx.channel().remoteAddress().toString().substring(1).split(":")[0];
		String port = ctx.channel().remoteAddress().toString().substring(1).split(":")[1];
		SocketClient.setOrDelServerChannel(address + ":" + port, null);
		Product product = createFixProduct(address, port, 1);
		DeviceManagerCache.getDeviceManagerClient().updateProtocolStatus(product);
		super.channelInactive(ctx);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf byteBuf = (ByteBuf) msg;
		String address = ctx.channel().remoteAddress().toString().substring(1).split(":")[0];
		String port = ctx.channel().remoteAddress().toString().substring(1).split(":")[1];
		MsgDispatcher.getCodecAdapter(ProtocolConstant.TCP_SERVER).decode(address, port, byteBuf);
		super.channelRead(ctx, msg);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		super.channelReadComplete(ctx);
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		super.userEventTriggered(ctx, evt);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		String address = ctx.channel().remoteAddress().toString().substring(1).split(":")[0];
		String port = ctx.channel().remoteAddress().toString().substring(1).split(":")[1];
		SocketClient.setOrDelServerChannel(address + ":" + port, null);
		Product product = createFixProduct(address, port, 1);
		DeviceManagerCache.getDeviceManagerClient().updateProtocolStatus(product);
		super.exceptionCaught(ctx, cause);
	}


	private Product createFixProduct(String address, String port, Integer online) {
		Product product = new Product();
		product.setIpAddr(address);
		product.setPort(Integer.valueOf(port));
		product.setProtocol(1);
		product.setOnline(online);
		product.setUpdateTime(new Date());
		return product;
	}

}
